package com.rock.${systemCode}.web.${subSystemCode}.controller;

import java.util.Date;


import java.util.List;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rock.common.base.BaseController;
import com.rock.common.dto.Msg;
import com.rock.common.dto.Query;
import com.rock.common.util.AESUtil;
import com.rock.common.util.StringUtil;
import com.rock.upms.model.UpmsDepartment;
import com.rock.upms.web.common.util.UserUtil;
import com.rock.${systemCode}.api.model.service.I${className}Service;
import com.rock.${systemCode}.model.${className};
import com.rock.${systemCode}.model.${className}Example;
import com.rock.${systemCode}.model.${className}Example.Criteria;
import com.rock.${systemCode}.web.common.config.WebConfig;
import com.rock.${systemCode}.web.common.util.UserUtil;

@Controller
@RequestMapping(value = "${systemCode}/${subSystemCode}")
public class ${className}Controller extends BaseController {

	private static Logger log = LoggerFactory.getLogger(${className}Controller.class);
	@Autowired
	private I${className}Service ${caseName}Service;

	/**
	 * 功能： 列表初始化<br>
	 * 说明：点击菜单或链接进行列表初始化，初始化默认查询条件等<br>
	 * 请求方式：链接（GET）
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @return 返回视图
	 */
	@RequestMapping(value = "/list",method = RequestMethod.GET)
	@RequiresRoles("login")//登录访问，方便测试，请据实修改
	//@RequiresPermissions(value = "${systemCode}:${subSystemCode}:list")
	public String toList(Model model) {
		Query query = new Query(1, WebConfig.getPageSize(), true);// 构造函数按默认分页方式分页，从第一页开始
		//query.add("isDelete", "0");
		model.addAttribute("query", query);
		return "${systemCode}/${subSystemCode}/${caseName}";
	}

	/**
	 * 功能：列表查询<br>
	 * 说明：<br>
	 * （1）列表初始化完成后显示初始化数据<br>
	 * （2）页面查询条件发生改变后的查询<br>
	 * 请求方式：ajax post请求
	 * 
	 * @param query
	 * @param request
	 * @param response
	 * @return Msg的json串
	 */
	@ResponseBody
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@RequiresRoles("login")//登录访问，方便测试，请据实修改
	//@RequiresPermissions(value = "${systemCode}:${subSystemCode}:list")
	public Msg list(Query query) {
		int pageNum = query.getPageNum();// 页号
		int dataNum = query.getPageSize();// 每页显示记录数
		int navigatePages = WebConfig.getPageNavigatePages();// 导航显示页数
		if (!query.getIsPagination()) {
			pageSize=0;//不分页
		}
		// 封装查询条件（自动生成三个，请根据实际修改）
		//String isDelete = (String) query.getExtend().get("isDeleted");// 是否删除
		//String usercode = (String) query.getExtend().get("usercode");// 机构号
		//String username = (String) query.getExtend().get("username");// 机构名模糊匹配
		String orderBy = " ${pkCode} desc ";// 默认ID降序
		${className}Example ${caseName}Example = new ${className}Example();
		Criteria criteria = ${caseName}Example.or();
//		if (StringUtil.isNotBlank(isDelete)) {
//			criteria.andIsDeletedEqualTo(isDelete);
//		}
//		if (StringUtil.isNotBlank(usercode)) {
//			criteria.andUsercodeEqualTo(usercode);
//		}
//		if (StringUtil.isNotBlank(username)) {
//			criteria.andUsernameLike("%" + username + "%");
//		}
		${caseName}Example.setOrderByClause(orderBy);
		PageInfo<${className}> page = ${caseName}Service.selectByExampleForStartPage(${caseName}Example, pageNum, pageSize, navigatePages);
		if(page!=null){
			return Msg.success("").add("pageInfo", page);
		}else{
			return Msg.fail("获取数据失败");
		}
	}

	/**
	 * 功能：新增（初始化）<br>
	 * 请求方式：ajax get
	 * 
	 * @return Msg的json串
	 */
	@ResponseBody
	//@RequiresPermissions("${systemCode}:${subSystemCode}:add")
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public Msg toAdd() {
		${className} vo = new ${className}();
		//vo.setCreateUsercode(UserUtil.getUsercode());
		//vo.setCreateUsername(UserUtil.getUsername());
		//vo.setCreateTime(new Date());
		return Msg.success("").add("${caseName}", vo);
	}

	/**
	 * 功能：新增（保存）<br>
	 * 请求方式：ajax
	 * 
	 * @param vo
	 * @return Msg的json串
	 */
	@ResponseBody
	@RequiresRoles("login")//登录访问，方便测试，请据实修改
	//@RequiresPermissions("${systemCode}:${subSystemCode}:add")
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Msg add(${className} vo) {
		log.trace("--->add vo:" + vo.toString());
		//vo.setCreateUsercode(UserUtil.getUsercode());
		//vo.setCreateUsername(UserUtil.getUsername());
		//vo.setCreateTime(new Date());
		//vo.setIsDeleted("0");
		int result = ${caseName}Service.insert(vo);
		if (result == 0) {
			return Msg.fail("保存失败");
		} else {
			return Msg.success("保存成功");
		}
	}

	/**
	 * 功能：删除 <br>
	 * 说明：根据主键ID删除记录，ID未加密
	 * 
	 * @param cryptId
	 * @return Msg的json串
	 */
	@ResponseBody
	@RequiresRoles("login")//登录访问，方便测试，请据实修改
	//@RequiresPermissions("${systemCode}:${subSystemCode}:delete")
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
	public Msg delete(@PathVariable("id") String id) {
		int idint = StringUtil.getInt(id);
		if (idint == -1) {
			return Msg.fail("你操作的数据不存在");
		} else {
			int backint = ${caseName}Service.deleteByPrimaryKey(idint);
			if (backint != 0) {
				return Msg.success("删除成功");
			} else {
				return Msg.fail("删除失败");
			}
		}
	}

	/**
	 * 功能：修改（初始化）<br>
	 * 说明：根据主键获取记录，主键未加密<br>
	 * 请求方式：ajax
	 * 
	 * @param id
	 * @return Msg的json串
	 */
	@ResponseBody
	@RequiresRoles("login")//登录访问，方便测试，请据实修改
	//@RequiresPermissions("${systemCode}:${subSystemCode}:edit")
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public Msg toEdit(@PathVariable("id") String id) {
		${className} vo = null;
		int idint = StringUtil.getInt(id);
		if (idint == -1) {
			return Msg.fail("你操作的数据不存在");
		} else {
			vo = (${className}) ${caseName}Service.selectByPrimaryKey(idint);
		}
		return Msg.success("").add("${caseName}", vo);
	}

	/**
	 * 功能：修改（保存）<br>
	 * 请求方式：ajax
	 * 
	 * @param vo
	 * @return Msg的json串
	 */
	@ResponseBody
	@RequiresRoles("login")//登录访问，方便测试，请据实修改
	//@RequiresPermissions("${systemCode}:${subSystemCode}:edit")
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public Msg edit(${className} vo) {
		log.trace("--->edit vo:" + vo.toString());
		int result = ${caseName}Service.updateByPrimaryKey(vo);
		if (result == 0) {
			return Msg.fail("保存失败");
		} else {
			return Msg.success("保存成功");
		}
	}

	/**
	 * 功能：查看<br>
	 * 说明：根据主键获取记录，主键未加密<br>
	 * 请求方式：ajax
	 * 
	 * @param id
	 * @return Msg的json串
	 */
	@ResponseBody
	@RequiresRoles("login")//登录访问，方便测试，请据实修改
	//@RequiresPermissions("${systemCode}:${subSystemCode}:read")
	@RequestMapping(value = "/read/{id}", method = RequestMethod.GET)
	public Msg read(@PathVariable("id") String id) {
		${className} vo = null;
		int idint = StringUtil.getInt(id);
		if (idint == -1) {
			return Msg.fail("你操作的数据不存在");
		} else {
			vo = (${className}) ${caseName}Service.selectByPrimaryKey(idint);
		}
		return Msg.success("").add("${caseName}", vo);
	}
}